package com.aruba.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
@ComponentScan("com.aruba")
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Order(1)
public class MyAspect {

    @Pointcut("execution(* com.aruba.mapper.*.add*(..))")
    public void addPointcut() {
    }

    @Before("addPointcut()")
    public void methodBefore(JoinPoint joinPoint) {
        System.out.println("MyAspect methodBefore..");
        System.out.println(Arrays.toString(joinPoint.getArgs()));
    }

    @After("addPointcut()")
    public void methodAfter(JoinPoint joinPoint) {
        System.out.println("MyAspect methodAfter..");
    }

    //returning 指定返回值对应增强方法的参数名
    @AfterReturning(pointcut = "addPointcut()", returning = "ret")
    public void methodAfterReturning(JoinPoint joinPoint, Object ret) {
        System.out.println("MyAspect methodAfterReturning..  :" + ret);
    }

    @AfterThrowing(pointcut = "addPointcut()", throwing = "ex")
    public void methodAfterThrowing(JoinPoint joinPoint, Exception ex) {
        System.out.println("MyAspect methodAfterThrowing..  ex:" + ex);
    }

    @Around("addPointcut()")
    public Object methodAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("MyAspect methodAround..  start");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("MyAspect methodAround..  end");

        return proceed;
    }
}
